﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.DB.CoreDAO;
using ScheduleMaker.ConfigManager;
using ScheduleMaker.DB;

namespace ScheduleMaker
{
    public partial class smAdmSubjectEdit : Form
    {
        //IList<DB.Domain.EquipmentSchool> equipments;
        //DB.Domain.SubjectSchool subject;

        IList<DB.Domain.Core.Equipment> equipments;
        DB.Domain.Core.Subject subject;

        public smAdmSubjectEdit(DB.Domain.Core.Subject _subject)
        {
            InitializeComponent();
            this.subject = _subject;

            lblSubjectName.Text = this.subject.Name;

            this.getEquipmentList();
            this.getSubjectEquipmentList();
        }

        private void lblRoomName_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbEquipments.SelectedIndex;
            try
            {
                DB.Domain.Core.Equipment equipment = equipments.ElementAt(selectedIndex);
                SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                //DB.SubjectDAOSchool subjectDAO = new DB.SubjectDAOSchool();
               
                if (!subject.Equipments.Contains(equipment))
                {
                    this.subject.Equipments.Add(equipment);
                    subjectDAO.saveOrUpdate(subject);

                }
                else
                {
                    MessageBox.Show("Wyposażenie już dodano!", "Błąd " + selectedIndex);
                }

            }
            catch
            {
                MessageBox.Show("Nie wybrano wyposażenia!", "Błąd " + selectedIndex);
            }
            // aktualizacja listy
            this.getSubjectEquipmentList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // coś tam xD - gdy usuwamy 

            int selectedIndex = lbSubjectEquipment.SelectedIndex;
            try
            {
                if (MessageBox.Show("Czy na pewno chcesz usunąć ten przedmiot?", "Potwierdź usunięcie", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DB.Domain.Core.Equipment equipment = subject.Equipments.ElementAt(selectedIndex);

                    SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                    subject.Equipments.Remove(equipment);
                    subjectDAO.saveOrUpdate(subject);
                }
            }
            catch
            {
                MessageBox.Show("Nie wybrano wyposażenia!", "Błąd " + selectedIndex);
            }
            // odświeżenie listy
            this.getSubjectEquipmentList();
        }

        private void groupBox11_Enter(object sender, EventArgs e)
        {

        }

        // Pobieranie - przedmiotów
        private void getEquipmentList()
        {
            lbEquipments.Items.Clear();
            //DB.EquipmentDAOSchool equipmentDAO = new DB.EquipmentDAOSchool();
            EquipmentDAO equipmentDAO = DAOFactory.getEquipmentDAO(ConfigManager.ConfigManager.getConfiguration().Type);
            equipments = equipmentDAO.getAll();

            foreach (var elem in equipments)
            {
                lbEquipments.Items.Add(elem.Id + ": " + elem.Name);
                //lbEquipments.Items.Add("id: " + elem.Id + " Name: " + elem.Name);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }
        }

        // Pobieranie wyposażenia podpiętego do sali
        private void getSubjectEquipmentList()
        {
            lbSubjectEquipment.Items.Clear();

            foreach (var elem in this.subject.Equipments)
            {
                lbSubjectEquipment.Items.Add(elem.Id + ": " + elem.Name);
                //lbSubjectEquipment.Items.Add("id: " + elem.Id + " Name: " + elem.Name);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }

            /*
            //this.classroom = classroomDAO.getById(classroom.Id);
            foreach (var elem in subject.Equipments)
            {
                Console.WriteLine("EQ: " + elem.Id + " Name: " + elem.Name);
            }
             * */
        }


    }
}
